/*
 * Copyright (c) 2011 Robert Kirchgessner <rkirchge@gmail.com>
 *                    Carlo Pascoe <carlo.pascoe@gmail.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#pragma once
#ifndef _SUBGRAPHOVERLAY_H_

#include <stdint.h>
#include <sys/queue.h>
#include "graph.h"

/**
 * @define METHOD_DEGREE_PRIORITY
 * This method is a greedy method which attempts to construct subgraphs that
 * maximize total degrees of individual subgraphs. When selecting a vertice for a 
 * subgraph, it selects the highest degree vertice first. For subsequent vertices,
 * it attempts to select the highest degree vertice adjacent to any vertice in
 * the subgraph. In the case of a tie, it selects the vertice psuedo-randomly.:w
 */

/**
 * @define METHOD_RANDOM_PRIORITY
 * This method performs a random walk around the highest degree vertices. When constructing
 * a subgraph, it first selects the highest degree free vertice from the graph. When
 * selecting subsequent vertices, it performs a random walk around adjacent free vertices to
 * the subgraph.
 */

/**
 * @define METHOD_RANDOM
 * This method constructs subgraphs using a pure random-walk method. It first selects a 
 * random free vertice from the graph. It when performs a random walk around the selected
 * vertice to construct a subgraph.
 */

/**
 * @define METHOD_OLD
 * @deprecated
 * This method is deprecated. It perfomed a biased random walk around the highest degree vertice in the list.
 * This method is similar to METHOD_RANDOM_PRIORITY, but edges of the last vertice added to the
 * subgraph were biased.
 */

enum {METHOD_DEGREE_PRIORITY, METHOD_RANDOM_PRIORITY, METHOD_RANDOM, METHOD_OLD};

struct overlay_edge
{
  uint32_t flux;
  struct subgraph *sg;
  LIST_ENTRY(overlay_edge) next;
};

struct overlay_vertice
{
  struct vertice *v;
  LIST_ENTRY(overlay_vertice) next;
};

struct subgraph 
{
  LIST_HEAD(vertex_list, overlay_vertice) vertices;
  LIST_HEAD(edge_list, overlay_edge) edges;
  LIST_ENTRY(subgraph) next;
};

struct overlaygraph
{
  uint32_t size;
  LIST_HEAD(subgraph_list, subgraph) subgraphs;
};

void overlay_init(struct overlaygraph *og);

void overlay_cleanup(struct overlaygraph *og);

struct subgraph * overlay_insert_subgraph(struct overlaygraph *og);

void overlay_subgraph_edge(struct subgraph *sg0, struct subgraph *sg1);

void generate_subgraph_overlay(struct graph *g, struct overlaygraph *og, uint32_t method, uint32_t k);

void overlay_subgraph_add_vertice(struct subgraph *sg, struct vertice *v);

void overlay_print(struct overlaygraph *og);

void overlay_write_dotty(const char *fname, struct overlaygraph *og);

struct overlay_edge * overlay_get_edge(struct subgraph *sg0, struct subgraph *sg1);

void overlay_subgraph_adj(struct subgraph *sg, uint32_t *adj, uint32_t k);

#endif /*_SUBGRAPHOVERLAY_H_*/

